package xjh.him.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xjh.him.R;

import java.util.HashMap;
import java.util.LinkedList;

import xjh.him.bean.ChatBean;
import xjh.him.common.SharedData;
import xjh.him.entity.User;
import xjh.him.util.Utils;
import xjh.him.util.GlideUtil;
import xjh.him.util.LogcatUtil;

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
    private static final int TYPE_LEFT = 0;
    private static final int TYPE_RIGHT = 1;
    private LinkedList<ChatBean> beans;
    private HashMap<Integer, User> userMap;

    public ChatAdapter(LinkedList<ChatBean> beans, HashMap<Integer, User> userMap) {
        this.beans = beans;
        this.userMap = userMap;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_LEFT:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item_left, parent, false);
                break;
            case TYPE_RIGHT:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item_right, parent, false);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + viewType);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int pos) {
        ChatBean bean = beans.get(pos);
        User user = userMap.get(bean.getUserId());
        if (user == null) {
            LogcatUtil.w("user not exist, user_id:%d", bean.getUserId());
            return;
        }
        if (bean.isLeft()) {
            GlideUtil.loadImage(holder.curView.getContext(), user.getAvatarUrl(),
                    holder.leftAvatar, GlideUtil.defaultRoundedAvatarOpt());
            holder.leftContent.setBackgroundResource(R.drawable.bg_chat_left);
            setText(holder.curView.getContext(), holder.leftContent, bean);
        }else {
            GlideUtil.loadImage(holder.curView.getContext(), user.getAvatarUrl(),
                    holder.rightAvatar, GlideUtil.defaultRoundedAvatarOpt());
            holder.rightContent.setBackgroundResource(R.drawable.bg_chat_right);
            setText(holder.curView.getContext(), holder.rightContent, bean);
        }
    }

    private void setText(Context ctx, TextView textView, ChatBean bean) {
        if (bean.getType() == SharedData.CHAT_MSG_TYPE_TEXT) {
            textView.setText(bean.getText());
            int px = Utils.dip2px(ctx, 11);
            textView.setPadding(px, px, px, px);
        }else if (bean.getType() == SharedData.CHAT_MSG_TYPE_IMAGE) {
            if (!"".equals(bean.getFileUrl()))
                GlideUtil.setBitmap(ctx, bean.getFileUrl(), textView);
            else
                textView.setText(bean.getText());
            textView.setBackgroundResource(0);
            textView.setPadding(0, 0, 0, 0);
        }else if (bean.getType() == SharedData.CHAT_MSG_TYPE_FILE) {
            // TODO file content handler
        }
    }

    @Override
    public int getItemViewType(int pos) {
        return beans.get(pos).isLeft() ? TYPE_LEFT : TYPE_RIGHT;
    }

    @Override
    public int getItemCount() {
        return beans.size();
    }

    public LinkedList<ChatBean> getBeans() {
        return beans;
    }

    public HashMap<Integer, User> getUserMap() {
        return userMap;
    }

    public void addLast(ChatBean bean) {
        beans.addLast(bean);
        notifyItemInserted(beans.size()-1);
    }

    public void addAll(LinkedList<ChatBean> beanList) {
        beans.addAll(0, beanList);
        notifyItemRangeInserted(0, beanList.size());
    }

    public int getLastPosition() {
        return beans.size() == 0 ? 0 : beans.size()-1;
    }

    protected static class ViewHolder extends RecyclerView.ViewHolder {
        private View curView;
        private ImageView leftAvatar, rightAvatar;
        private TextView leftContent, rightContent;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            curView = itemView;
            leftAvatar = itemView.findViewById(R.id.left_avatar);
            rightAvatar = itemView.findViewById(R.id.right_avatar);
            leftContent = itemView.findViewById(R.id.left_content);
            rightContent = itemView.findViewById(R.id.right_content);
        }
    }
}